package com.dzf.controller;

import com.dzf.Util.SessionUtil;
import com.dzf.entity.Discuss;
import com.dzf.entity.RespJson;
import com.dzf.entity.User;
import com.dzf.service.DiscussService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/dis")
public class DiscussController {
    @Autowired
    DiscussService discussService;
    @Autowired
    SessionUtil sessionUtil;

    /**
     * 添加新的评论
     * @param discuss 评论对象，会通过level来判断是父评论还是子评论
     * @return 请求是否成功
     */
    @RequestMapping("/addDis")
    public RespJson addDis(@RequestBody Discuss discuss){
        boolean b = discussService.addDiscuss(discuss);
        if (b)
        {
            return RespJson.success(200,"OK",b);
        }
        return RespJson.success(200,"错误！",b);
    }


    @RequestMapping("/delDis")
    @ResponseBody
    public RespJson delDis(@RequestBody Discuss discuss)
    {
        Boolean b = discussService.delDiscuss(discuss);
        if (b)
        {
            return RespJson.success(200,"OK",b);
        }
        return RespJson.success(200,"错误！",b);
    }


    /**
     * 查询评论列表
     * @param blogid 博客的ID
     * @param pagenum 页码
     * num 定为六行
     * @return {total:总行数，list：查询的评论列表}
     */
    @RequestMapping("/findByDis/{blogid}/{pagenum}")
    @ResponseBody
    public RespJson findByDis(@PathVariable("blogid")String blogid,@PathVariable("pagenum") int pagenum)
    {
        Map<String, Object> map = discussService.findByDis(blogid, pagenum);
        return RespJson.success(200,"OK",map);
    }

    @RequestMapping("/findByUserDis/{pagenum}/{num}")
    @ResponseBody
    public RespJson findByUserDis(@PathVariable("pagenum")int pagenum,@PathVariable("num")int num){
        User user = sessionUtil.getUser();

        Map<Object, Object> map = discussService.findByUserDis(user.getId(), pagenum, num);
        return RespJson.success(200,"OK",map);
    }

}
